<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            箱码替换
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">送检单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="inspection" disabled="true">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">原箱码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描 原箱码" id="oldTm" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">新箱码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描 新箱码" id="newTm" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()">保存</div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";

    //初始化查询明细数据和下拉框下拉框
    apiready = function () {

        var inspection = api.pageParam.inspection;
        $("#inspection").val(inspection);

        $("#oldTm").focus();
        idSelector = "oldTm";
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            $("#" + idSelector).val(ret);

            autoSave();
        });
    }

    $("#oldTm").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            $("#oldTm").val(tm);
            autoSave();
        }
    });

    $("#newTm").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            $("#newTm").val(tm);
            autoSave();
        }
    });

    //扫描条码后保存
    function autoSave() {
        var oldTm = $("#oldTm").val();
        var newTm = $("#newTm").val();

        if (oldTm == "" || oldTm == null || oldTm == undefined) {
            return;
        }

        if (newTm == "" || newTm == null || newTm == undefined) {
            return;
        }

        save();
    }

    //保存
    function save() {
        //送检单号
        var inspection = $("#inspection").val();
        //取检测状态
        var oldTm = $("#oldTm").val();
        //取检测结果
        var newTm = $("#newTm").val();

        if (oldTm == "" || oldTm == null || oldTm == undefined) {
            api.toast({
                msg: "原箱码不能为空!",
                duration: 3000
            });
            return;
        }

        if (newTm == "" || newTm == null || newTm == undefined) {
            api.toast({
                msg: "新箱码不能为空!",
                duration: 3000
            });
            return;
        }

        //条码特殊字符处理
        //oldTm = encodeURIComponent(encodeURIComponent(oldTm));
        //newTm = encodeURIComponent(encodeURIComponent(newTm));

        api.showProgress({
            title: '保存中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/inspectionReplaceBoxSave";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                inspection: inspection,
                oldTm: oldTm,
                newTm: newTm,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == "0") {

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    $("#oldTm").val("");
                    $("#newTm").val("");

                    //失败
                } else if (myobj.code == "1") {

                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            } else {
                api.hideProgress();
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
        api.execScript({
            name: "uwmInspection",
            script: 'searchBindBox()'
        });
    }
</script>

</html>